package com.mysafe.provision.view.main;

import java.io.Serializable;
import java.util.Set;
import java.util.TreeSet;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;

import com.mysafe.provision.exception.ValidationException;
import com.mysafe.provision.model.account.Account;
import com.mysafe.provision.model.cycletype.CycleType;
import com.mysafe.provision.model.expense.Expense;
import com.mysafe.provision.model.intervaltype.IntervalType;
import com.mysafe.provision.service.expense.IExpenseService;
import com.mysafe.provision.service.tag.ITagService;
import com.mysafe.provision.view.login.LoginScreen;

@Named
@SessionScoped
public class MainScreen implements Serializable {
	
	private static final long serialVersionUID = -8894597247912325423L;
	
	private LoginScreen login;
	
	private Account loggedAccount;
	@Inject private Expense expense;
	private String tags;
	private boolean renderedIntervalType;
	
	private Set<CycleType> cycleTypes;
	private Set<IntervalType> intervalTypes;
	
	private IExpenseService expenseService;
	private ITagService tagService;
	
	public MainScreen() {
	}

	@Inject
	public MainScreen(IExpenseService expenseService, ITagService tagService, LoginScreen login) {
		this.expenseService = expenseService;
		this.tagService = tagService;
		this.login = login;
	}

	@PostConstruct
	private void init() {
		if (login.getAccount().isLogged()) {
			loggedAccount = login.getAccount();
		}
		login.finalize();
		cycleTypes = fillCycleTypes();
		intervalTypes = fillIntervalTypes();
		//FIXME reset bean by provider
		expense.getCycle().getCycleType().setId(CycleType.FIXED);
		renderedIntervalType = true;
	}
	
	private Set<CycleType> fillCycleTypes() {
		Set<CycleType> items = new TreeSet<CycleType>();
		items.add(new CycleType(CycleType.FIXED, "fixed"));
		items.add(new CycleType(CycleType.VARIED, "varied"));
		return items;
	}
	
	private Set<IntervalType> fillIntervalTypes() {
		Set<IntervalType> items = new TreeSet<IntervalType>();
		items.add(new IntervalType(IntervalType.YEAR, "year"));
		items.add(new IntervalType(IntervalType.MONTH, "month"));
		items.add(new IntervalType(IntervalType.DAY, "day"));
		return items;
	}
	
	public void valueChangeRadioCycleType(ValueChangeEvent event) {
		Integer newValue = (Integer)event.getNewValue();
		renderedIntervalType = newValue.equals(CycleType.FIXED);
		if (!renderedIntervalType) {
			expense.getCycle().getIntervalType().setId(null);
		}
	}
	
	public void save() {
		expense.setAccount(loggedAccount);
		try {
			expenseService.insert(expense);
		} catch (ValidationException e) {
			System.out.println(e.getErrors());
		}
	}
	
	public void setLoggedAccount(Account loggedAccount) {
		this.loggedAccount = loggedAccount;
	}

	public Account getLoggedAccount() {
		return loggedAccount;
	}

	public void setExpense(Expense expense) {
		this.expense = expense;
	}

	public Expense getExpense() {
		return expense;
	}

	public void setCycleTypes(Set<CycleType> cycleTypes) {
		this.cycleTypes = cycleTypes;
	}

	public Set<CycleType> getCycleTypes() {
		return cycleTypes;
	}

	public Set<IntervalType> getIntervalTypes() {
		return intervalTypes;
	}

	public void setIntervalTypes(Set<IntervalType> intervalTypes) {
		this.intervalTypes = intervalTypes;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
		expense.setTags(tagService.splitTags(tags));
	}

	public boolean isRenderedIntervalType() {
		return renderedIntervalType;
	}

	public void setRenderedIntervalType(boolean renderedIntervalType) {
		this.renderedIntervalType = renderedIntervalType;
	}
	
	


}
